package cn.edu.sasu.service.impl;

import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.common.constants.Constant;
import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.util.DateUtils;
import cn.edu.sasu.domain.entity.SysDept;
import cn.edu.sasu.domain.vo.DeptListVO;
import cn.edu.sasu.mapper.DeptMapper;
import cn.edu.sasu.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王尧
 * @since 2024-04-27
 */
@Service
public class DeptServiceImpl implements IDeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public LayuiRespResult findSysDeptAll(Integer page, Integer limit) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysDept> sysDepts = deptMapper.findSysDeptList(Constant.DELETE_FLAG_YES, offset, rows);
        Integer totalSize = deptMapper.countSysDept(Constant.DELETE_FLAG_YES);
        return LayuiRespResult.success(totalSize, sysDepts);
    }


    @Override
    public LayuiRespResult search(Integer page, Integer limit, String deptName) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysDept> sysDepts = deptMapper.searchSysDeptList(Constant.DELETE_FLAG_YES, offset, rows, deptName);
        Integer totalSize = deptMapper.countSearchSysDept(Constant.DELETE_FLAG_YES, deptName);
        return LayuiRespResult.success(totalSize, sysDepts);
    }

    @Override
    public RespResult save(SysDept dept) {
        SysDept sysDept= deptMapper.findSysDeptByDeptName(dept.getDeptName());
        if(sysDept != null){
            return RespResult.error("该部门已经存在，请重新输入！");
        }

        deptMapper.save(dept);
        return RespResult.success("添加成功");
    }

    @Override
    public LayuiRespResult findSysDepts() {
        List<DeptListVO> deptList=deptMapper.getAllDeptNames();
        int size = deptList.size();
        return LayuiRespResult.success(size,deptList);
    }


    @Override
    public RespResult delSysDeptByDeptId(Integer deptId) {
        deptMapper.updateSysDeptDeleteFlagByDeptId(deptId,Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    @Override
    public RespResult batchdel(Integer[] deptIds) {
        deptMapper.updateSysDeptDeleteFlagByDeptIds(Arrays.asList(deptIds),Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }


    @Override
    public RespResult updateInfo(SysDept dept) {
        dept.setUpdateDate(DateUtils.getCurrentDate());
        Integer integer = deptMapper.updateInfo(dept);
        if (integer > 0) {
            return RespResult.success("修改成功", deptMapper.findSysDeptByDeptId(dept.getDeptId()));
        } else {
            return RespResult.error("修改失败");
        }
    }
}
